﻿#include "HpBar.h"
#include "GameUtil.h"

using namespace cocos2d;

HpBar* HpBar::create(float width, float height, float max)
{
	HpBar *pRet = new HpBar();
	if (pRet && pRet->init()) {
        pRet->autorelease();

		pRet->d_max = max;

		float border = 1.0f;

		CCLayerColor* borderLayer = CCLayerColor::create(ccc4(0, 0, 0, 255), width, height);
		pRet->addChild(borderLayer);
		borderLayer->setAnchorPoint(CCPointZero);
		borderLayer->setPosition(CCPointZero);

		width -= border*2;
		height -= border*2;
		CCLayerGradient* baseHealthLayer = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(0, 255, 0, 255), ccp(1.0f,0.0f));
		pRet->addChild(baseHealthLayer);
		baseHealthLayer->setAnchorPoint(CCPointZero);
		baseHealthLayer->setPosition(ccp(border, border));
		baseHealthLayer->setContentSize(CCSize(width, height));

        pRet->d_healthHiderLayer = CCLayerColor::create(ccc4(0, 0, 0, 255), width, height);
		pRet->addChild(pRet->d_healthHiderLayer);
		pRet->d_healthHiderLayer->setAnchorPoint(ccp(1.0f, .0f));
		pRet->d_healthHiderLayer->setPosition(ccp(border, border));

		pRet->setContentSize(borderLayer->getContentSize());

        return pRet;
    } else {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

HpBar::~HpBar(void)
{
}

void HpBar::setHp(float hp)
{
	if(hp < 0) {
		hp = .0f;
	}
	if(hp > d_max) {
		hp = d_max;
	}

	d_healthHiderLayer->setScaleX((d_max - hp ) / d_max);
}
